import api from '~api/index'
import util from '~src/lib/util'
export default {
  state: {
    list: {
      data: [],
      page: 1,
      id:'-1',
      code:'0',
      total:0
    },
    show: {},
    vshow: {}
  },
  mutations: {
    ['setGassList'] (state, data) {
      if (data.code == '200') {
        if (data.page === 1) {
          data.data = [].concat(data.data)
          if (data.data.length === 0) {
            data.data = []
          }
        } else {
          data.data = state.list.data.concat(data.data)
        }
        state.list = data
        state.list.total = data.total
      }
      state.list.code = data.code
    },
    ['setGassInfo'] (state, data) {
      state.show = data
    },
    ['setGassvInfo'] (state, data) {
      state.vshow = data
    }
  },
  actions: {
    async ['getGassList'] ({commit},config) {
      const { data: { data, code } } = await api.post('/v1/gasstationList')
      if (data && code === '200') {
        commit('setGassList', {
          data: data.gs_list.length>0?data.gs_list:[],
          page: config.page,
          total:data.total,
          code: code
        })
        config.callback && config.callback()
      }else{
        console.log(code)
        commit('setGassList', {
          data: [],
          page: config.page,
          code: code,
          total:data.total,
          code: code
        })
        config.stopCallback && config.stopCallback()
      }
    },
    async ['getGassInfo'] ({commit}, gsId) {
      const { data: { data, code } } = await api.post('/v1/getGasstationInfo',{gsId:gsId,gasNo:'1'})
      if (data && code === '200') {
        commit('setGassInfo', data)
        commit('setGassvInfo', data.gasno)
      }
    }
  },
  getters: {
    ['getGassList'] (state) {
      return state.list
    },
    ['getGassInfo'] (state) {
      return state.show
    },
    ['getGassvInfo'] (state) {
      return state.vshow
    }
  }
}
